package com.nidu.demo.log.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.log.convertor.OperateLogConvertor;
import com.nidu.demo.log.dto.OperateLogCO;
import com.nidu.demo.log.gateway.OperateLogGateway;
import com.nidu.demo.log.model.OperateLog;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 操作日志单个查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class OperateLogSingleQryExe {

    private final OperateLogGateway gateway;
    private final OperateLogConvertor convertor;

    public SingleResponse<OperateLogCO> execute(Long id) {
        // 1. 根据ID查询操作日志
        OperateLog operateLog = gateway.getById(id);
        if (operateLog == null) {
            throw new BizException(ErrorCodeConstants.OPERATE_LOG_NOT_FOUND.getCode(), ErrorCodeConstants.OPERATE_LOG_NOT_FOUND.getMessage());
        }

        // 2. 转换为DTO返回
        OperateLogCO operateLogCO = convertor.toClientObject(operateLog);
        return SingleResponse.of(operateLogCO);
    }

}
